package com.peach.controller;

import com.peach.pojo.User;
import com.peach.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/qianduan")
public class QianDuanController {

    @Autowired
    private UserService userService;

    @GetMapping("/login2")
    public String login2(){
        return "login";
    }

    @PostMapping("/login")
    public String login(User user, Model model, HttpServletRequest request ,Integer optionsRadios){

        if(optionsRadios==0){
            Integer userId = user.getUserId();
            String userPwd = user.getUserPwd();
          //  User user1 = userService.selectOneUser(userId);
         //   System.out.println(userId+"   "+userPwd+"  "+user1);
            if(userId==123 && "123".equals(userPwd)){
                return "redirect:/index/selectAllActivities3";
            }else{
                model.addAttribute("msg","用户名或密码错误！");
                return "redirect:/index2";
            }
        }else{
            Integer userId = user.getUserId();
            String userPwd = user.getUserPwd();
            User user1 = userService.selectOneUser(userId);
            System.out.println(userId+"   "+userPwd+"  "+user1);
            if(user1!=null && userPwd.equals(user1.getUserPwd())){
                request.getSession().setAttribute("userGmx",user1.getUserName());
                request.getSession().setAttribute("user",user1);
                return "redirect:/index/selectAllActivities2";
            }else{
                model.addAttribute("msg","用户名或密码错误！");
                return "redirect:/index2";
            }
        }
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("userGmx");
        return "/login";
    }
}
